#include "editclockdialog.h"

int stringToInt(QString str);
bool isMusic(QString str);

EditClockDidalog::EditClockDidalog(QWidget *parent, ZygTimer* zygTimer)
    :QDialog(parent)
{

    int btnInterval = 0;

    this->setWindowTitle("设置时间");
    this->setFixedSize(240,150);

    //添加窗体背景色
    ZygBackground editBackground(EDIT_BACKGROUND);
    this->setPalette(editBackground);

    QLineEdit *hourEdit = new QLineEdit(this);
    hourEdit->setPlaceholderText("时");
    hourEdit->setFixedSize(50,25);

    QLabel *label1 = new QLabel;
    label1->setText(":");
    label1->setFixedSize(10,25);

    QLineEdit *minuteEdit = new QLineEdit(this);
    minuteEdit->setPlaceholderText("分");
    minuteEdit->setFixedSize(50,25);


    QLabel *label2 = new QLabel;
    label2->setText(":");
    label2->setFixedSize(10,25);


    QLineEdit *secondEdit = new QLineEdit(this);
    secondEdit->setPlaceholderText("秒");
    secondEdit->setFixedSize(50,25);

    int editX = (this->width() - hourEdit->width() - label1->width() - minuteEdit->width() - label2->width() - secondEdit->width()) / 2;
    int editY = 40;
    hourEdit->move(editX,editY);
    label1->move(editX + 50,editY);
    minuteEdit->move(editX + 60,editY);
    label2->move(editX + 110,editY);
    secondEdit->move(editX + 120,editY);

    /****赋初值******/
    ZygTimer *zyg = zygTimer;
    if(zyg == nullptr)
    {
        zyg = new ZygTimer;
    }
    else
    {
        hourEdit->setText(zygTimer->getHourString());
        minuteEdit->setText(zygTimer->getMinuteString());
        secondEdit->setText(zygTimer->getSecondString());
    }

    //铃声文件指定
//    QPushButton *fileBtn = new QPushButton("铃声",this);
//    fileBtn->move((this->width() - fileBtn->width())/2 ,100);
//    connect(fileBtn,&QPushButton::clicked,[=](){
//        zyg->setSoundFilePath(QFileDialog::getOpenFileName());
//    });



    QPushButton *ok = new QPushButton("确定",this);
    QPushButton *cancel = new QPushButton("取消",this);
    ok->move((this->width() - ok->width() - cancel->width() - btnInterval) / 2,this->height() - ok->height());
    cancel->move(ok->x() + ok->width() + btnInterval ,this->height() - cancel->height());

    //取消被点击
    connect(cancel,&QPushButton::clicked,[=](){
        this->close();
    });

    //当确定被点击
    connect(ok,&QPushButton::clicked,[=](){
        int hour = stringToInt(hourEdit->text());
        if(hour < 0 || hour > 23){
            QMessageBox::critical(this,"小时格式有误","小时值应在0~23之间");
            return;
        }
        int minute = stringToInt(minuteEdit->text());
        if(minute < 0 || minute > 59){
            QMessageBox::critical(this,"分钟格式有误","分钟值应在0~59之间");
            return;
        }
        int second = stringToInt(secondEdit->text());
        if(second < 0 || second > 59){
            QMessageBox::critical(this,"秒格式有误","秒值应在0~59之间");
            return;
        }

        zyg->setHour(hour);
        zyg->setMinute(minute);
        zyg->setSecond(second);

//        if(!isMusic(zyg->getSoundFilePath()))
//        {
//            QMessageBox::critical(this,"音频格式有误","请选择一个.mp3文件作为音频");
//            return;
//        }
        ZygTimer::addTimer(zyg);
        emit this->closed();
        this->close();
    });
}



bool isMusic(QString str)
{
    return str != nullptr && str.endsWith(".mp3");
}

EditClockDidalog::~EditClockDidalog(){

}
